var verify = sessionStorage.getItem('verify');
var character = sessionStorage.getItem('character');

var hId = sessionStorage.getItem('hId');
var title = sessionStorage.getItem('title');
var item = sessionStorage.getItem('item');
var deadLine = sessionStorage.getItem('deadLine');

//登录判断
switch (verify) {
    case 'true':
        var name = sessionStorage.getItem('name');
        var account = sessionStorage.getItem('account');
        document.getElementById("title").innerText = title;
        document.getElementById('deadLine').innerText = "截止日期：" + deadLine;
        $("#div_info").append("")
        break;

    case 'false':
        alert('请登录！');
        location.href = "login.html";
        break;
}

switch (character) {
    case 'teacher':
        $.ajax({
            url: '/homework?method=findAllSubmit',
            type: 'get',
            data: {hId: hId},
            dataType: 'json',
            success: function (result) {
                //json{stuName, stuAccount, sItem, sTime, sFileUrl}
                var i;
                for (i = 0; i < result.length; i++) {
                    var stuAccount = result[i].stuAccount;
                    var stuName = result[i].stuName;
                    var sTime = result[i].sTime;
                    var sFileUrl = result[i].sFileUrl;
                    var sFileName = result[i].sFileName;
                    var downBtnString;

                    //如果没有文件
                    if (sFileUrl == "") {
                        downBtnString = "<a name='href' href='#' class='btn btn-primary' data-url='" + sFileUrl + "' disabled='disabled'>下载";
                    }
                    //如果有文件
                    else {
                        downBtnString = "<a name='href' href='#' class='btn btn-primary' " +
                            "data-url='" + sFileUrl + "' data-fileName='" + sFileName + "' onclick='downloadFile(this)'>下载";
                    }
                    $('#tableBody').append("<tr>" +
                        "<td class='text-center' >" + stuAccount + "" +
                        "</td>" +
                        "<td class='text-center'>" + stuName + "" +
                        "</td>" +
                        "<td class='text-center'>" + sTime + "" +
                        "</td>" +
                        "<td class='text-center'>" + sFileName + "" +
                        "<td class='text-center'>" +
                        downBtnString +
                        "</a>" +
                        "</td>" +
                        "</tr>"
                    );
                }
            }
        });

        break;
}

//文件下载
function downloadFile(a) {
    var fileName = $(a).data('filename');

    var form = $("<form></form>").attr("action", "/homework/downloadHomework").attr("method", "post");
    form.append($("<input>").attr("type", "hidden").attr("name", "fileName").attr("value", fileName));
    form.appendTo('body').submit().remove();
}

//一键全部下载
function allDownload(btn) {
    var form = $("<form></form>").attr("action", "/homework/downloadAllHomework").attr("method", "post");
    form.append($("<input>").attr("type", "hidden").attr("name", "hId").attr("value", hId));
    form.appendTo('body').submit().remove();
}
